package cn.ahxb.frame.work.ui.home.activities;

import android.graphics.Color;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.ahxb.frame.R;
import cn.ahxb.frame.common.adapter.GeneralFragmentPagerAdapter;
import cn.ahxb.frame.common.base.BaseActivity;
import cn.ahxb.frame.work.ui.home.fragments.HomePage1Fragment;
import cn.ahxb.frame.work.ui.home.fragments.HomePage2Fragment;
import cn.ahxb.frame.work.ui.home.fragments.HomePage3Fragment;
import cn.ahxb.frame.work.ui.home.fragments.HomePage4Fragment;

/**
 * 主页面
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.vp_fragment)
    ViewPager mVpFragment;
    @BindView(R.id.rg_tab)
    RadioGroup mRgTab;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        requestTranslucentStatusBar(Color.TRANSPARENT, true);
        initTabBar();
    }

    /**
     * 初始化tab 栏
     */
    private void initTabBar() {
        mFragments.add(new HomePage1Fragment());
        mFragments.add(new HomePage2Fragment());
        mFragments.add(new HomePage3Fragment());
        mFragments.add(new HomePage4Fragment());
        mVpFragment.setAdapter(new GeneralFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        mRgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_0:
                        mVpFragment.setCurrentItem(0, false);
                        break;
                    case R.id.rb_1:
                        mVpFragment.setCurrentItem(1, false);
                        break;
                    case R.id.rb_2:
                        mVpFragment.setCurrentItem(2, false);
                        break;
                    case R.id.rb_3:
                        mVpFragment.setCurrentItem(3, false);
                        break;
                }
            }
        });
        mVpFragment.setOffscreenPageLimit(4);
        //默认选中第一个页面
        mRgTab.check(R.id.rb_0);
    }

    @Override
    protected void initData() {
    }


    private static final long DELAY_TIME = 1500;
    long lastPressBackKeyTime = 0;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastPressBackKeyTime < DELAY_TIME) {
            super.onBackPressed();
        } else {
            lastPressBackKeyTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
        }
    }

    public void checkMoudle(int position) {
        switch (position) {
            case 0:
                mRgTab.check(R.id.rb_0);
                break;
            case 1:
                mRgTab.check(R.id.rb_1);
                break;
            case 2:
                mRgTab.check(R.id.rb_2);
                break;
            case 3:
                mRgTab.check(R.id.rb_3);
                break;
        }
    }
}